package com.ss.warfarin.persistence.dao;

import java.sql.*;

import javax.servlet.http.HttpServlet;

import com.ss.warfarin.common.util.DbUtils;
import com.ss.warfarin.persistence.model.User;

public class LoginDao extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public User checkUser(String username, String password) {
		
		User user = null;
		
		try {
			
			// Connection DB  from DbUtils
			Connection connection = DbUtils.getConnection();
			
			// Query database
			String sql = "SELECT username, password FROM user WHERE username = ? AND password = MD5(?)";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				user = new User();
				user.setUsername(username);
			}

			// Close connection to database
			ps.close();
			rs.close();
			DbUtils.closeConnection(connection);

		} catch (Exception e) {
			System.out.println("ERROR:" + e.getMessage());
		}
		
		return user;
	}
	
}
